package com.guchenbo.example.io.pipe;

import java.io.IOException;
import java.io.PipedOutputStream;

/**
 * Created by CreGu on 2016/6/13.
 */
public class Sender extends Thread{
    private final PipedOutputStream outputStream = new PipedOutputStream();

    @Override
    public void run() {
        System.out.println("sending........");
//        writerShortMessage();
        writerLongMessage();
    }

    private void writerShortMessage() {
        String s = "a short message";
        try {
            outputStream.write(s.getBytes());
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    private void writerLongMessage() {
//        longer than 1024
        StringBuilder sb = new StringBuilder();
        for (int i=0;i<204;i++) {
            sb.append("0123456789");
        }
        sb.append("abcde");

        try {
            outputStream.write(sb.toString().getBytes());
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    public PipedOutputStream getOutputStream() {
        return outputStream;
    }

    public static void main(String[] args) {
        StringBuilder sb = new StringBuilder();
        for (int i=0;i<102;i++) {
            sb.append("0123456789");
        }
        sb.append("abcde");
        System.out.println(sb.toString().getBytes().length);
    }
}
